﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Windows.Input;
using Stala;
using System.ComponentModel;
using System.Windows;
using System.IO;

namespace TestApp
{
	class VersionVM : INotifyPropertyChanged
	{
		private Version version;
		private Version latestVersion;
		private string sourceUri = "Insert path of the version file or URI of version";
		private string downloadUri = "Insert URI of the file to be downloaded";
		private string localPathWhereWeStoreWhatWeDownload = "Insert path to store the download";
		private bool downloadUri_IsSet = false;
		private bool localPathWhereWeStoreWhatWeDownload_IsSet = false;
		private CheckVersionCommand checkVersionCommand;
		private DownloadVersionCommand downloadVersionCommand;
		private InstallCommand installCommand;

		public string Version
		{
			get { return version.ToString(); }
		}

		public string LatestVersion
		{
			get
			{
				if (latestVersion != null)
					return latestVersion.ToString();
				else
					return "Unknown";
			}
		}

		public string Uri
		{
			get { return sourceUri; }
			set
			{
				sourceUri = value;
				var FullUri = new Uri(sourceUri);
				checkVersionCommand.VersionFinder = StalaFactory.GetSimpleVersionFinder(FullUri);
			}
		}

		public string DownloadUri
		{
			get { return downloadUri; }
			set
			{
				downloadUri = value;
				downloadUri_IsSet = true;
				UpdateVersionDownloader();
			}
		}

		public string Destination
		{
			get { return localPathWhereWeStoreWhatWeDownload; }
			set
			{
				localPathWhereWeStoreWhatWeDownload = value;
				if(new Uri(localPathWhereWeStoreWhatWeDownload).IsFile)
					localPathWhereWeStoreWhatWeDownload_IsSet = true;
				else
					localPathWhereWeStoreWhatWeDownload_IsSet = false;
				UpdateVersionDownloader();
				UpdateInstaller();
			}
		}

		public string Title { get; private set; }
		
		public ICommand CheckVersion { get { return checkVersionCommand; } }
		public ICommand Download { get { return downloadVersionCommand; } }
		public ICommand Install { get { return installCommand; } }

		internal VersionVM()
		{
			version = this.GetType().Assembly.GetName().Version;
			
			Title = "Stala test application version " + Version;
			installCommand = new InstallCommand();
			checkVersionCommand = new CheckVersionCommand();
			checkVersionCommand.NewVersionFound += UpdateLatestVersion;

			downloadVersionCommand = new DownloadVersionCommand();
			downloadVersionCommand.DownloadFailed += downloadFailed;
		}

		void downloadFailed(Exception exception)
		{
			MessageBox.Show("Download failed. Exeption Message: " + exception.Message);
		}

		void UpdateLatestVersion(Version Version)
		{
			latestVersion = Version;
			if (PropertyChanged != null)
			{
				PropertyChanged(this, new PropertyChangedEventArgs("LatestVersion"));
			}
		}

		void UpdateVersionDownloader()
		{
			if (downloadUri_IsSet && localPathWhereWeStoreWhatWeDownload_IsSet)
			{
				downloadVersionCommand.VersionDownloader = StalaFactory.GetSimpleVersionDownloader(new Uri(downloadUri), new Uri(localPathWhereWeStoreWhatWeDownload));
				downloadVersionCommand.VersionDownloader.DownloadCompleted += UpdateInstaller;
			}
			else
			{
				if (downloadVersionCommand.VersionDownloader != null)
				{
					downloadVersionCommand.VersionDownloader.DownloadCompleted -= UpdateInstaller;
					downloadVersionCommand.VersionDownloader = null;
				}
			}
		}

		void UpdateInstaller()
		{
			if (localPathWhereWeStoreWhatWeDownload_IsSet && File.Exists(localPathWhereWeStoreWhatWeDownload))
				installCommand.Installer = StalaFactory.GetSimpleVersionInstaller(new Uri(localPathWhereWeStoreWhatWeDownload));
			else
				installCommand.Installer = null;
		}

		public event PropertyChangedEventHandler PropertyChanged;
	}
}
